package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.VocBookConverter;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.VocBookDO;
import com.cskaoyan.wordmemorize.dao.mapper.VocBookMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteBookOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocBookDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocBookCommand;
import com.cskaoyan.wordmemorize.request.VocBookPageRequest;
import com.cskaoyan.wordmemorize.service.VocBookService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VocBookServiceImpl implements VocBookService {
    @Resource
    private VocBookConverter vocBookConverter;

    @Resource
    private VocBookMapper vocBookMapper;
    @Override
    public PageDTO<VocBookDTO> getBookPage(VocBookPageRequest pageQuery) {
        LambdaQueryWrapper<VocBookDO> queryWrapper = new LambdaQueryWrapper<>();
        //获取keyword和书籍类目Id
        //判断keyword是否为空
        if (pageQuery.getKeyword() != null) {
            queryWrapper
                    .like(VocBookDO::getName, pageQuery.getKeyword())
                    .eq(VocBookDO::getBcId, pageQuery.getCategoryId())
            ;
        }else {
            queryWrapper.eq(VocBookDO::getBcId, pageQuery.getCategoryId());
        }

        //判断是否分页
        if (pageQuery.getPageNum() == null || pageQuery.getCount() == null) {
            //进行全表查询
            List<VocBookDO> vocBookDOS = vocBookMapper.selectList(queryWrapper);
            return vocBookConverter.vocBookList2Page(vocBookDOS, 1L);
        }
        Page<VocBookDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
        //查找所有书籍
        Page<VocBookDO> result =  vocBookMapper.selectPage(page, queryWrapper);
        PageDTO<VocBookDTO> pageDTO = vocBookConverter.vocBookList2Page(result.getRecords(), page.getTotal());

        return pageDTO;
    }

    @Override
    public Long addVocBook(VocBookCommand command) {
        VocBookDO vocBookDO = vocBookConverter.vocBookCommand2VocBookDO(command);
        vocBookMapper.insert(vocBookDO);
        return null;
    }

    @Override
    public void updateVocBook(VocBookCommand command) {
        VocBookDO vocBookDO = vocBookConverter.vocBookCommand2VocBookDO(command);
        vocBookMapper.updateById(vocBookDO);
    }

    @Override
    public DeleteBookOperateDTO deleteVocBook(Long bookId) {
        DeleteBookOperateDTO deleteBookOperateDTO = vocBookConverter.vocBookDO2DeleteBookDTO(vocBookMapper.selectById(bookId));
        vocBookMapper.deleteById(bookId);
        return null;
    }

    @Override
    public List<DeleteBookOperateDTO> deleteVocBooks(BatchDeleteCommand command) {
        List<DeleteBookOperateDTO> deleteBookOperateDTOS = vocBookConverter.vocBookDOs2DeleteBookDTOs(vocBookMapper.selectBatchIds(command.getIds()));
        vocBookMapper.deleteBatchIds(command.getIds());
        return null;
    }
}
